package com.takepoints.mobile;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.takepoints.android.bo.Card;
import com.takepoints.android.services.TPServices;
import com.takepoints.mobile.zxing.CaptureActivity;
import com.takepoints.android.util.DownloadImageTask;

public class MyOfferDetail extends BaseActivity {
	
	private int cardId;
	private String siteName;
	private boolean isFromTakeNow = false;
	
	private Card card = null;
	
	private final String IMG_PATH = "https://www.takepoints.com/public/uploads/mobile/";
	
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mycoupondetail);
        manageMenu(2);

        Bundle bundle = this.getIntent().getExtras();
        cardId = Integer.parseInt(bundle.getString("cardId"));
        siteName = bundle.getString("siteName");
        try{
        	isFromTakeNow = "Y".equals(bundle.getString("takeNow"));
        } catch(Exception e){ }
        
        TextView lblHeader = (TextView)findViewById(R.id.lblHeader);
        if(siteName != null){
        	lblHeader.setText(siteName);
        } else{
        	lblHeader.setText(R.string.title_myoffer_detail);
        }
        processOfferDetail();
	}
	
	private void processOfferDetail(){
		final ProgressDialog dialog = ProgressDialog.show(this, "Takepoints.com", "Loading Detail ...", true);
		final Handler handler = new Handler() {
        	public void handleMessage(Message msg) {
        		if(card != null){
        			TextView txtItemName = (TextView)findViewById(R.id.txtItemName);
        			txtItemName.setText(card.getItemName());
        			txtItemName.setTextColor(Color.parseColor("#555555"));
        			//txtItemName.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        			
        			ImageView imgItem = (ImageView)findViewById(R.id.imgItem);
        			if(imgItem != null){
        				Bitmap bitmap = BitmapFactory.decodeFile(IMG_CACHE_PATH + card.getImageName());
        				if(bitmap != null){
        					imgItem.setImageBitmap(bitmap);
        				} else{
        					imgItem.setTag(IMG_PATH + card.getImageName());
            				new DownloadImageTask().execute(imgItem);
        				}
        			}
        			
        			TextView txtCouponExpire = (TextView)findViewById(R.id.txtCouponExpire);
        			txtCouponExpire.setText("ภายในวันที่ " + card.getEndDate());
        			
        			LinearLayout lnrContent = (LinearLayout)findViewById(R.id.lnrContent);
				    /*
        			TableRow tr1 = new TableRow(lnrContent.getContext());
        	    	tr1.setBackgroundResource(R.drawable.info_middle);
        	    	TableRow.LayoutParams tr1Param = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 50);
        	    	tr1Param.setMargins(1, 4, 1, 4);
        	    	tr1.setLayoutParams(tr1Param);
	        	    	TextView txtExpiryDate = new TextView(tr1.getContext());
	        	    	txtExpiryDate.setText("ภายในวันที่  " + card.getEndDate());
	        	    	txtExpiryDate.setTextSize(13);
	        	    	txtExpiryDate.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
	        	    	txtExpiryDate.setTextColor(Color.parseColor("#555555"));
	        	    	txtExpiryDate.setMinimumHeight(42);
	        	    	txtExpiryDate.setPadding(5, 3, 5, 3);
	        	    	txtExpiryDate.setGravity(Gravity.CENTER_VERTICAL);
	        	    tr1.addView(txtExpiryDate);
	        	    lnrContent.addView(tr1);
    				*/
				    TableRow tr2 = new TableRow(lnrContent.getContext());
        	        tr2.setBackgroundResource(R.drawable.info_middle);
        	    	TableRow.LayoutParams tr2Param = new TableRow.LayoutParams( TableRow.LayoutParams.FILL_PARENT
        	    															  , TableRow.LayoutParams.WRAP_CONTENT);
        	    	tr2Param.setMargins(1, 4, 1, 6);
        	    	tr2.setLayoutParams(tr2Param);
	        	        TextView txtOfferLine1 = new TextView(tr2.getContext());
	        	        txtOfferLine1.setText(card.getOfferLine1());
	        	        txtOfferLine1.setTextSize(11);
	        	        txtOfferLine1.setTextColor(Color.GRAY);
	        	        txtOfferLine1.setHeight(LayoutParams.WRAP_CONTENT);
	        	        txtOfferLine1.setPadding(5, 3, 5, 3);
	        	        txtOfferLine1.setSingleLine(false);
	        	        txtOfferLine1.setGravity(Gravity.TOP);
	        	        tr2.addView(txtOfferLine1);
	        	    lnrContent.addView(tr2);
        		}
        		dialog.dismiss();
        		if(isFromTakeNow){
        			popup("ใช้คูปอง..เรียบร้อยแล้ว");
        		} else{
            		popup("กด Take .. เพื่อใช้คูปองนี้");
        		}
        	}
		};
        Thread thread = new Thread(){  
        	public void run(){
        		card = TPServices.getMyItemDetail(cardId);
        		handler.sendEmptyMessage(0);
        	}
        };
        thread.start();
	}
	
	protected void processTake(Context ctx){
		PackageManager pckMng = getPackageManager();
		if (pckMng != null && pckMng.hasSystemFeature(PackageManager.FEATURE_NFC)) {
			checkNfcStatus();
		} else{
			gotoVerifyCode();
		}
	}
	
	@TargetApi(10)
	private void checkNfcStatus(){
		NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter();
    	if(nfcAdapter.isEnabled()){
    		gotoVerifyCodeNFC();
    	} else{
    		confirmNetworkSetting();
    	}
	}
	
	private void confirmNetworkSetting(){
		final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
		alertbox.setMessage("NFC ถูกปิดอยู่ !\n\nคุณต้องการเปิด NFC ใช่หรือไม่ ?");
		alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dlgInf, int which) {
				gotoNetworkSetting();
			}
		});
		alertbox.setNeutralButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dlgInf, int which) {
				gotoVerifyCode();
			}
		});
		alertbox.show();
	}
    private final void gotoNetworkSetting(){
    	startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
    }
    
    private final void gotoVerifyCode(){
    	Intent intent = new Intent(this, CaptureActivity.class);
    	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    	intent.putExtra("cardId", card.getItemId());
    	intent.putExtra("siteName", siteName);
    	intent.putExtra("pageSrc", "MyOfferDetail");
		startActivity(intent);
    }
	
	private void gotoVerifyCodeNFC(){
    	Intent intent = new Intent(this, VerifyCodeNFC.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("cardId", card.getItemId());
		intent.putExtra("siteName", siteName);
		intent.putExtra("pageSrc", "MyOfferDetail");
		startActivity(intent);
    }
	
}
